package org.japo.java.matisse.samples;

import java.awt.Color;
import java.awt.Font;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import org.japo.java.entities.Rotulo;

/**
 *
 * @author Joël García Cervera
 */
public class EstiloAsistido extends javax.swing.JFrame {

    Rotulo r;
    
    /**
     * Creates new form EstiloAsistido
     */
    public EstiloAsistido() {
        initComponents();
        
        String texto = lblTexto.getText();
        Font fuente = lblTexto.getFont();
        Color colorFrente = lblTexto.getForeground();
        Color colorFuente = lblTexto.getBackground();
        r = new Rotulo(texto, fuente, colorFrente, colorFuente);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlTexto = new javax.swing.JPanel();
        lblTexto = new javax.swing.JLabel();
        pnlEstilo = new javax.swing.JPanel();
        cbxNegrita = new javax.swing.JCheckBox();
        cbxCursiva = new javax.swing.JCheckBox();
        pnlBotones = new javax.swing.JPanel();
        btnGuardar = new javax.swing.JButton();
        btnCargar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Estilo asistido");

        pnlTexto.setBackground(new java.awt.Color(255, 255, 255));
        pnlTexto.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        lblTexto.setBackground(new java.awt.Color(255, 255, 255));
        lblTexto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblTexto.setText("Texto de prueba");

        javax.swing.GroupLayout pnlTextoLayout = new javax.swing.GroupLayout(pnlTexto);
        pnlTexto.setLayout(pnlTextoLayout);
        pnlTextoLayout.setHorizontalGroup(
            pnlTextoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblTexto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnlTextoLayout.setVerticalGroup(
            pnlTextoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTextoLayout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(lblTexto)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pnlEstilo.setBorder(javax.swing.BorderFactory.createTitledBorder("Estilo"));

        cbxNegrita.setText("Negrita");
        cbxNegrita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxNegritaActionPerformed(evt);
            }
        });

        cbxCursiva.setText("Cursiva");
        cbxCursiva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxCursivaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlEstiloLayout = new javax.swing.GroupLayout(pnlEstilo);
        pnlEstilo.setLayout(pnlEstiloLayout);
        pnlEstiloLayout.setHorizontalGroup(
            pnlEstiloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEstiloLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlEstiloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbxNegrita)
                    .addComponent(cbxCursiva, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        pnlEstiloLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cbxCursiva, cbxNegrita});

        pnlEstiloLayout.setVerticalGroup(
            pnlEstiloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEstiloLayout.createSequentialGroup()
                .addComponent(cbxNegrita)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbxCursiva))
        );

        pnlBotones.setBorder(javax.swing.BorderFactory.createTitledBorder("Botones"));

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCargar.setText("Cargar");

        javax.swing.GroupLayout pnlBotonesLayout = new javax.swing.GroupLayout(pnlBotones);
        pnlBotones.setLayout(pnlBotonesLayout);
        pnlBotonesLayout.setHorizontalGroup(
            pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btnGuardar)
            .addComponent(btnCargar)
        );

        pnlBotonesLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCargar, btnGuardar});

        pnlBotonesLayout.setVerticalGroup(
            pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBotonesLayout.createSequentialGroup()
                .addComponent(btnGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCargar))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlEstilo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlTexto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {pnlBotones, pnlEstilo});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlTexto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlEstilo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {pnlBotones, pnlEstilo});

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-212)/2, (screenSize.height-242)/2, 212, 242);
    }// </editor-fold>//GEN-END:initComponents

    private void cbxNegritaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxNegritaActionPerformed
        Font f = r.getFuente();
        String tipografia = f.getFamily();
        int estilo = generarEstilo();
        int talla = f.getSize();
        f = new Font(tipografia, estilo, talla);
        r.setFuente(f);
//        System.out.println("Estilo: " + estilo);
    }//GEN-LAST:event_cbxNegritaActionPerformed

    private void cbxCursivaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxCursivaActionPerformed
        Font f = r.getFuente();
        String tipografia = f.getFamily();
        int estilo = generarEstilo();
        int talla = f.getSize();
        f = new Font(tipografia, estilo, talla);
        r.setFuente(f);
//        System.out.println("Estilo: " + estilo);
    }//GEN-LAST:event_cbxCursivaActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        try {
            guardarConfiguracion();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error de guardar fichero.");
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EstiloAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EstiloAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EstiloAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EstiloAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EstiloAsistido().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCargar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JCheckBox cbxCursiva;
    private javax.swing.JCheckBox cbxNegrita;
    private javax.swing.JLabel lblTexto;
    private javax.swing.JPanel pnlBotones;
    private javax.swing.JPanel pnlEstilo;
    private javax.swing.JPanel pnlTexto;
    // End of variables declaration//GEN-END:variables
    
    
    public void establecerEstilo(int estilo) {
        if(estilo == Font.PLAIN) {
            cbxNegrita.setSelected(false);
            cbxCursiva.setSelected(false);
        } else if(estilo == Font.BOLD) {
            cbxNegrita.setSelected(true);
            cbxCursiva.setSelected(false);
        } else if(estilo == Font.ITALIC) {
            cbxNegrita.setSelected(false);
            cbxCursiva.setSelected(true);
        } else {
            cbxNegrita.setSelected(true);
            cbxCursiva.setSelected(true);
        }
    }
    
    public void guardarConfiguracion() throws Exception {
        String FICHERO = "../salida.txt";
        PrintWriter salida = null;
        String linea = "";
        
        try {
            salida = new PrintWriter(new FileWriter(FICHERO));
            int estilo = r.getFuente().getStyle();
            linea += estilo;
            salida.println(linea);
        } finally {
            if(salida != null) {
                salida.close();
            }
        }
    }

    private int generarEstilo() {
        int negrita = Font.PLAIN;
        if(cbxNegrita.isSelected()) {
            negrita = Font.BOLD;
        }
        
        int cursiva = Font.PLAIN;
        if(cbxCursiva.isSelected()) {
            cursiva = Font.ITALIC;
        }
        
        return negrita + cursiva;
    }
}
